<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreBrand;
use App\Http\Requests\UpdateBrand;
use App\Models\Shop\Brand;
use Illuminate\Http\Request;


class BrandController extends Controller
{

    public function __construct()
    {
        view()->share([
            '_shop'=>'am-in',
            '_brand'=>'am-active',
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //模糊查询
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }
        };

        $brands = Brand::where($where)->orderBy('sort_order', 'desc')->paginate(config('admin.page_size'));
        return view('admin.shop.brand.index', compact('brands'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.brand.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreBrand $request)
    {
        Brand::create($request->all());
        return redirect('/admin/shop/brand')->with('success', '新增品牌成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $brand = Brand::find($id);
        return view('admin.shop.brand.edit', compact('brand'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateBrand $request, $id)
    {
        $brand = Brand::find($id);
        $brand->update($request->all());
        return redirect(route('shop.brand.index'))->with('success', '编辑品牌成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Brand::destroy($id);
    }

    /**
     * 多选删除
     */
    public function delete_all(Request $request)
    {
        Brand::destroy($request->check_id);
    }

    /**
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $brand = Brand::find($request->id);
        $attr = $request->attr;
        $brand->$attr = !$brand->$attr;
        $brand->save();
    }

    /**
     * 商品排序
     */
    public function sort_order(Request $request)
    {
        $brand = Brand::find($request->id);
        $brand->sort_order = $request->sort_order;
        $brand->save();
    }
}
